package com.furongji.education.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.furongji.common.result.Result;
import com.furongji.education.domain.po.Banner;
import com.furongji.education.service.BannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@Tag(name = "轮播图后台管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/courses")
public class BannerAdminController {

    private final BannerService bannerService;

    @Operation(summary = "获取轮播图列表")
    @GetMapping("/pageBanner/{current}/{size}")
    public Result pageBannerList(@PathVariable(value = "current") Long current, @PathVariable(value = "size") Long size) {
        IPage<Banner> page = new Page<>(current, size);
        IPage<Banner> result = bannerService.page(page);
        Map<String, Object> map = new HashMap<>();
        map.put("records", result.getRecords());
        map.put("total", page.getTotal());
        return Result.success(map);
    }

    @Operation(summary = "添加轮播图")
    @PostMapping("/addBanner")
    public Result addBanner(@RequestBody Banner banner) {
        boolean flag = bannerService.save(banner);
        return flag ? Result.success() : Result.error();
    }

    @Operation(summary = "根据id获取轮播图")
    @GetMapping("/getBannerById/{id}")
    public Result getBannerById(@PathVariable String id) {
        Banner banner = bannerService.getById(id);
        return Result.success(banner);
    }

    @Operation(summary = "修改轮播图")
    @PutMapping("/updateBanner")
    public Result updateBanner(@RequestBody Banner banner) {
        boolean flag = bannerService.updateById(banner);
        return flag ? Result.success() : Result.error();
    }

    @Operation(summary = "删除轮播图")
    @DeleteMapping("/deleteBanner/{id}")
    public Result deleteBanner(@PathVariable String id) {
        boolean flag = bannerService.removeById(id);
        return flag ? Result.success() : Result.error();
    }
}
